package com.example.utils;

import com.example.exception.RedisOpsResultIsNullException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
@Slf4j
public class RedisUtil {
    private static StringRedisTemplate redisTemplate;
    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static boolean delete(String key) {
        log.info("delete(...) => key -> {}", key);
        // 返回值只可能为true/false, 不可能为null
        Boolean result = redisTemplate.delete(key);
        log.info("delete(...) => result -> {}", result);
        return result;
    }
    public static long delete(Collection<String> keys) {
        log.info("delete(...) => keys -> {}", keys);
        Long count = redisTemplate.delete(keys);
        log.info("delete(...) => count -> {}", count);
        return count;
    }
    public static boolean hasKey(String key) {
        log.info("hasKey(...) => key -> {}", key);
        Boolean result = redisTemplate.hasKey(key);
        log.info("hasKey(...) => result -> {}", result);
        return result;
    }
    public static boolean expire(String key, long timeout, TimeUnit unit) {
        log.info("expire(...) => key -> {}, timeout -> {}, unit -> {}", key, timeout, unit);
        Boolean result = redisTemplate.expire(key, timeout, unit);
        log.info("expire(...) => result is -> {}", result);
        if (result == null) {
            throw new RedisOpsResultIsNullException();
        }
        return result;
    }
    public static boolean expireAt(String key, Date date) {
        log.info("expireAt(...) => key -> {}, date -> {}", key, date);
        Boolean result = redisTemplate.expireAt(key, date);
        log.info("expireAt(...) => result is -> {}", result);
        if (result == null) {
            throw new RedisOpsResultIsNullException();
        }
        return result;
    }
    public static long getExpire(String key) {
        Long result = getExpire(key, TimeUnit.SECONDS);
        return result;
    }
    public static long getExpire(String key, TimeUnit unit) {
        log.info("getExpire(...) =>key -> {}, unit is -> {}", key, unit);
        Long result = redisTemplate.getExpire(key, unit);
        log.info("getExpire(...) => result ->  {}", result);
        return result;
    }
    public static void set(String key, String value) {
        log.info("set(...) => key -> {}, value -> {}", key, value);
        redisTemplate.opsForValue().set(key, value);
    }
    public static void set(String key, String value,long expiration,TimeUnit timeUnit) {
        log.info("set(...) => key -> {}, value -> {}", key, value);
        redisTemplate.opsForValue().set(key, value,expiration,timeUnit);
    }
    public static String get(String key) {
        String value = redisTemplate.opsForValue().get(key);
        log.info("get(...) => key -> {}, value -> {}", key,value);
        return value;
    }

}
